import axios from 'axios'
import cookie from 'react-cookies'
import { message } from 'antd'
// 配置项
const axiosOption = {
  baseURL: 'http://localhost:3000/api',
  timeout: 10000
}
// 创建一个单例
const instance = axios.create(axiosOption);

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
  const userInfo = cookie.load("userInfo") || []
  const path = window.location.href
  console.log(window.location.href);
  if(!userInfo && path!=='/login'){
    message.warning('您当前未登陆，即将跳转到登录页');
    setTimeout(() => {
      window.location.href = "/login"
    }, 2000);
  }else{
    return config;
  }
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response.data;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});

export default instance;
